package me.shurufa.net.okhttp.callback;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.kaopiz.kprogresshud.KProgressHUD;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import me.shurufa.R;
import me.shurufa.model.Error;
import me.shurufa.response.base.BaseResp;
import me.shurufa.utils.Utils;

/**
 * Create by andy (https://github.com/andyxialm)
 * Create time: 16/5/11 23:11
 * Description : 看啥啊你  明显的网络请求回调。看毛线啊看 扯犊子
 */
public abstract class HttpCallback<T> extends BaseHttpRequestCallback<T> {

    private Context mContext;
    private KProgressHUD mProgressView;
    private String mLoadingText;

    /**
     * 这是一个不正经 的请求回调
     */
    public HttpCallback() {
    }

    /**
     * 这是一个不正经 的请求回调，显示转圈ProgressView
     * @param context
     */
    public HttpCallback(Context context) {
        mContext = context;
    }

    /**
     * 这是一个不正经 的请求回调，显示转圈ProgressView 并显示文字
     * @param context
     * @param loadingText
     */
    public HttpCallback(Context context, String loadingText) {
        mContext = context;
        mLoadingText = loadingText;
    }

    /**
     * 这是一个不正经 的请求回调，显示转圈ProgressView 并显示文字
     * @param context
     * @param resId
     */
    public HttpCallback(Context context, int resId) {
        mContext = context;
        mLoadingText = context.getString(resId);
    }

    public abstract void onRespSuccess(T t);
    public abstract void onRespFailure(T t);

    @Override
    protected void onSuccess(T t) {
        try {
            super.onSuccess(t);
            if (!(t instanceof BaseResp)) {
                onRespFailure(t);
                return;
            }
            if (((BaseResp) t).success) {
                onRespSuccess(t);
            } else {
                Error error = ((BaseResp) t).error;
                if (error != null && !TextUtils.isEmpty(error.message)) {
                    Utils.showToast(error.message);
                }
                onRespFailure(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        super.onFailure(errorCode, msg);
        Utils.showToast(R.string.network_error);
    }

    @Override
    public void onStart() {
        try {
            super.onStart();
            if (mContext != null) {
                showProgress();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void showProgress() {
        try {
            mProgressView = KProgressHUD.create(mContext);
            mProgressView.setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
            mProgressView.setCancellable(false);
            mProgressView.setAnimationSpeed(2);
            mProgressView.setDimAmount(0.5f);
            if (!TextUtils.isEmpty(mLoadingText)) {
                mProgressView.setLabel(mLoadingText);
            }
            mProgressView.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFinish() {
        try {
            super.onFinish();
            boolean isActivity = mContext instanceof Activity;
            boolean isFinished = isActivity ? ((Activity) mContext).isFinishing() : false;
            if (mContext != null && !isFinished && mProgressView != null) {
                mProgressView.dismiss();
                mProgressView = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
